package com.dgz.csmall.product.controller;

import com.dgz.csmall.product.pojo.param.CategoryAddNewParam;
import com.dgz.csmall.product.pojo.param.CategoryUpdateInfoParam;
import com.dgz.csmall.product.service.CategoryService;
import com.dgz.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author duguozheng
 * @Date 2023/05/11  23:31
 * @Description BrandController
 * @Version 1.0
 */
@RestController
@RequestMapping("/categories/")
@Api(tags = "01. 类别管理模块")
@Slf4j
@Validated
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("addNew")
    @ApiOperation("新增类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理【新增类别】的请求，参数：{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        log.debug("处理【新增类别】的请求，完成！");
        return JsonResult.ok();
    }



    @PostMapping("/delete")
    @ApiOperation("删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "删除类别失败！请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【删除类别】请求，参数：{}",id);
        categoryService.delete(id);
        log.debug("处理【删除类别】请求成功！");
        return JsonResult.ok();
    }
    @PostMapping("/updateInfo")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    public JsonResult updateInfo(@Range(min = 1, message = "删除类别失败！类别ID必须为正整数！")
                                 @RequestParam long id,
                                 @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【修改类别】请求，参数：{}",id);
        categoryService.updateInfoById(id,categoryUpdateInfoParam);
        log.debug("处理【修改类别】请求成功！");
        return JsonResult.ok();
    }
}
